﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PrimitiveEngine
{
    public class Vector2Ext
    {
        /// <summary>
        /// Finds the normal from two vectors
        /// </summary>
        /// <param name="first">The first vector</param>
        /// <param name="second">The second vector</param>
        /// <returns></returns>
        public static Vector2 FindEdgeNormal(Vector2 first, Vector2 second)
        {
            //Xbox360 need this variable to be initialized to Vector2.Zero
            Vector2 normal = Vector2.Zero;

            Vector2 temp = new Vector2(first.X - second.X, first.Y - second.Y);

            normal.X = -temp.Y; // get 2D normal
            normal.Y = temp.X; // works only on counter clockwise polygons

            normal.Normalize();

            return normal;
        }

        public static Vector2 FindVertexNormal(Vector2 first, Vector2 second, Vector2 third)
        {
            Vector2 temp;
            Vector2 one = FindEdgeNormal(first, second);
            Vector2 two = FindEdgeNormal(second, third);

            Vector2.Add(ref one, ref two, out temp);
            temp.Normalize();
            return temp;
        }
    }
}
